/*
题目:最长不含重复字符的字符串
请从字符串中找出一个最长的不包含重复字符的子字符串，计算该最长子字符串的长度。
https://leetcode.cn/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof/
 */
public class Offer48 {
    public int lengthOfLongestSubstring(String s) {
        Set<Character> set = new HashSet<> ();
        int res = 0, cnt = 0;
        int left = 0; //滑动窗口左指针
        int len = s.length();

        for (int i = 0; i < len; i++) {
            char ch = s.charAt(i);
            if (set.contains(ch)) { //如果包含这个元素
                while (s.charAt(left) != ch) {
                    set.remove(s.charAt(left));
                    left++;
                }
                left++;
                cnt = set.size();
            } else {
                set.add(ch);
                cnt++;
                res = Math.max(res, cnt);
            }
        }

        return res;
    }
}
